<?php
/**
 * Created by PhpStorm.
 * User: guo
 * Date: 2017/6/13
 * Time: 23:05
 */
namespace core;
class eric
{
    public static $classMap = [];           //定义引入的类。
    public $assign;
    public static function run()
    {
        $route           = new \core\lib\route();
        $controllerClass = $route->controller;
        $action          = $route->action;
        $controllerFile  = APP.'/controller/'.$controllerClass.'Controller.php';
        #### $controllerClass = \app\controller\indexController;
        $controllerClass = '\\' . MODULE . '\controller\\' . $controllerClass . 'Controller';
        if(is_file($controllerFile))
        {
            include $controllerFile;
            $ctrl = new $controllerClass();
            $ctrl->$action();
        }else
        {
            throw new \Exception('找不到'.$controllerClass.'控制器T_T');    //抛出异常
        }
    }

    /**
     * 自动加载类库
     *
     * @param $class   类名  $class = '\core\route';
     *                       ERIC.'/core/route.php';
     */
    public static  function load($class)
    {
        if(isset($classMap[$class]))
        {
            return true;
        }else
        {
            //1、将反斜线转为正斜线
            $class = str_replace('\\','/',$class);
            $file  = ERIC.'/'.$class.'.php';
            if(is_file($file))
            {
                include $file;
                self::$classMap[$class] = $class;
            }else
            {
                return false;
            }
        }
    }


    /**
     * 渲染试图
     *
     * @param $data
     */
    public function assign($name,$value)
    {
        $this->assign[$name] = $value;
    }
    
    public function display($file)
    {
        $file = APP.'/views/'.$file;
        if(is_file($file))
        {
//            p($this->assign);exit;
            extract($this->assign);
            include $file;
        }
    }
}